<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Ajax</title>
</head>
<body>
<p>
    <input type="button" value="发送" onclick="send();">
    <input type="button" value="发送" id="btn">
</p>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" crossorigin="anonymous"></script>
<script>
    function send() {
        $.post(
            "/ajax",
            {
                "name": "张三",
                "age": 23
            },
            function (data) {
                console.log(typeof (data));
                data = $.parseJSON(data); // str->json
                console.log(typeof (data));
                console.log(data.code);
                console.log(data.msg);
            }
        );
    }

    document.getElementById("btn").onclick = function () {
        // 1.创建 ajax核心对象 XMLHttpRequest
        const xhr = new XMLHttpRequest()
        // 2.注册回调函数
        xhr.onreadystatechange = function () {
            if (this.readyState == 4) {
                if (this.status == 200) {
                    let data = this.responseText;
                    console.log(data)
                    data = JSON.parse(data)
                    console.log(data)
                    console.log(data.code)
                    console.log(data.msg)
                }
            }
        }
        xhr.open("post", "/ajax", true);
        xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded")
        xhr.send("name=zs&age=13")
    }
</script>
</body>
</html>